﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Mengsk.ERP.App.Price
{
    /// <summary>
    /// PriceUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class PriceUserControl : UserControl, IUIPlugin
    {
        private Price[] tmallPrices = null;

        public PriceUserControl()
        {
            InitializeComponent();
        }

        public string Title
        {
            get { return "价格表计算"; }
        }

        public Control UIControl
        {
            get { return this; }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.dgvTMallPrice.ItemsSource != null)
            {
                return;
            }

            List<Price> tempList = new List<Price>();

            tempList.Add(new Price { StartPrice = 0, EndPrice = 100, AddPrice = 80 });
            tempList.Add(new Price { StartPrice = 100, EndPrice = 110, AddPrice = 85 });

            for (int i = 110, addPrice = 90; i <= 295; i += 5, addPrice += 5)
            {
                tempList.Add(new Price { StartPrice = i, EndPrice = i + 5, AddPrice = addPrice });
            }
            this.tmallPrices = tempList.ToArray();
            this.dgvTMallPrice.ItemsSource = tmallPrices;
        }

        private void SetTextBoxEmpty(params TextBox[] textBoxs)
        {
            foreach (var textBox in textBoxs)
            {
                textBox.Text = "";
            }
        }

        private void tbTMallPrice_TextChanged(object sender, TextChangedEventArgs e)
        {
            string orgnalPrice = this.tbMallOrginalPrice.Text.Trim();

            if (string.IsNullOrWhiteSpace(orgnalPrice))
            {
                this.SetTextBoxEmpty(this.tbMallSellPrice, this.tbMallShowPrice, this.tbTMallMallPrice);
                return;
            }

            try
            {
                //拿价
                int money = int.Parse(orgnalPrice);
                //查找当前的钱
                var price = this.tmallPrices.FirstOrDefault(obj => money >= obj.StartPrice && money <= obj.EndPrice);
                if (price == null)
                {
                    MessageBox.Show("没有合适的值价格表");
                    this.SetTextBoxEmpty(this.tbMallSellPrice, this.tbMallShowPrice, this.tbTMallMallPrice);
                    return;
                }
                //要出售的价
                money = price.AddPrice + money;

                //将该数据到8
                if (money % 10 >=2)
                {
                    money = (money / 10) * 10 + 8;
                }
                else
                {
                    money = (money / 10 - 1) * 10 + 8;
                }
                this.tbMallSellPrice.Text = money.ToString();

                //一口价
                money = (int)(money / 0.4);
                this.tbMallShowPrice.Text = money.ToString();

                //市场价
                this.tbTMallMallPrice.Text = (money * 2).ToString();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                this.SetTextBoxEmpty(this.tbMallSellPrice, this.tbMallShowPrice, this.tbTMallMallPrice, this.tbMallOrginalPrice);
            }
        }

        private void tbWangGouPrice_TextChanged(object sender, TextChangedEventArgs e)
        {
        }
    }
}
